package sound;

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

import resources.FileLoader;

public class Player implements Runnable{

	private URL theFileURL;
	private AudioClip theClip;
	private int soundLength; // should be in milliseconds
	private boolean loop;
	private boolean paused = false;
	private boolean alive = true;
	private Thread theThread;


	public Player(String pPathSong, boolean pLoop){
		super();
		theFileURL = FileLoader.chargeFichier(pPathSong);

		this.setSongLength((int)getCalcSongLenght());

		this.setLoop(pLoop);
		theClip = Applet.newAudioClip(theFileURL);
		//theThread = new Thread(this,"Thread du son ("+theFileURL+")");
	}

	@Override
	public void run() {
	//	System.out.println("Player.run");
		if(this.loop){
			playLoop();
		}
		else{
			play();
		}
	}

	public void playLoop(){
		while(isAlive()){
			while(!paused && isAlive()){
				this.play();
			}
			this.stop();
			break;
		}
	}

	public void play(){
		if(application.Utils.soundActivated){
			theClip.play();
		}
		try {
			Thread.sleep(this.soundLength);
		} catch (InterruptedException e) {
			//e.printStackTrace();
		}
	}

	public void resume(){
		this.paused = false;
		theThread = new Thread(this,"nouveau Thread du son : "+this.getFileURL());
		this.start();
	}

	public void pause() {
		this.paused = true;
		this.stop();
	}

	private void start() {
		this.theThread.start();
	}


	@SuppressWarnings("deprecation")
	public void stop(){
		//System.out.println("Player.stop");
		theClip.stop();
		if(theThread instanceof Thread){
			theThread.stop();
		}
		else{
			//System.err.println("Player.stop() > on n'a pas pu faire theThread.stop()");
		}
	}

	public URL getFileURL() {
		return theFileURL;
	}

	public void setFileURL(URL pTheFileURL) {
		this.theFileURL = pTheFileURL;
	}

	public int getSoundLength() {
		return soundLength;
	}


	public void setSongLength(int pSoundLengthMilliSecond) {
		this.soundLength = pSoundLengthMilliSecond;
	}

	public boolean isLoop() {
		return loop;
	}

	public void setLoop(boolean pLoop) {
		this.loop = pLoop;
	}

	public AudioClip getTheClip() {
		return theClip;
	}

	public long getCalcSongLenght() {
		long returned = 1000;

		try{
			Clip clip = AudioSystem.getClip();
			AudioInputStream ais = AudioSystem.getAudioInputStream(this.getFileURL());
			clip.open(ais);
			returned = clip.getMicrosecondLength();
		}
		catch(Exception e){
			//application.Utils.soundActivated = false;
			System.err.println("on désactive le son dans utils");
			try {
				this.finalize();
			} catch (Throwable e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return returned / 1000;
	}

	public boolean isPaused() {
		return paused;
	}

	public void setPaused(boolean paused) {
		this.paused = paused;
	}

	public boolean isAlive() {
		return alive;
	}

	public void setAlive(boolean alive) {
		this.alive = alive;
	}
	
	public void killSound(){
		this.setAlive(false);
		this.stop();
	}
}